#!/bin/bash

function init(){
   echo "---------------"
   echo "  INIT        "
   echo "---------------"
   executionTests="$1"
   thisFile=$0
   pythonDirConf=`dirname $0`
   pythonDirConf=`readlink -f $pythonDirConf`
   JOB_HOME=`readlink -f ./`
   OUTPUT_DIR="$JOB_HOME/target/"
   SITE_JOB=$OUTPUT_DIR/site
   TEST_REPORTS=$OUTPUT_DIR/nosetests.xml
   COVERAGE_REPORT=$OUTPUT_DIR/coverage.xml
   rm -Rf $SITE_JOB
   mkdir -p $SITE_JOB
   export PATH=$PROJECT_PLUGINS/pipeline_plugin:$PATH
}

function isExecutedInDevelenv(){
   if [ "`id -nu`" == "develenv" ]; then
      isDevelenv="true"
   else
      isDevelenv="false"
   fi
}

function unitTest(){
   . dp_unitTest.sh "$executionTests"
}

function metrics(){
   . dp_metrics.sh
}

function publishInRpmRepository(){
   local directory=$1
   local rpmFile
   for rpmFile in `find $directory -name "*.rpm"`; do
      $PROJECT_HOME/bin/publishRPM.sh $rpmFile
   done;
}

function package(){
   echo "---------------"
   echo "  RPM          "
   echo "---------------"
   python setup.py bdist_rpm 
   result=$?
   if [ $result != 0 ]; then
       echo Error en la creación del rpm
       exit 1
   fi 
   rm -Rf build $OUTPUT_DIR/dist/
   mv dist $OUTPUT_DIR
   publishInRpmRepository $OUTPUT_DIR/dist/
}

function rpmInfo(){
   local rpmFile
   local rpmFiles=$1
   FILE_RPM_INFO=$SITE_JOB/rpmInfo.txt
   echo -e ">>> INFORMATION:" >$FILE_RPM_INFO
   for rpmFile in $(find $OUTPUT_DIR/dist -name "*.rpm"); do
      rpm -qip $rpmFile >>$FILE_RPM_INFO
      echo -e "\n>>> DEPENDENCIES:"  >>$FILE_RPM_INFO
      rpm -qp --requires $rpmFile >>$FILE_RPM_INFO
      echo -e "\n>>> FILES:" >>$FILE_RPM_INFO
      rpm -qlp $rpmFile >>$FILE_RPM_INFO
   done;
   echo "---------------"
   echo "  RPM INFO     "
   echo "---------------"
   cat $FILE_RPM_INFO
}

function duplications(){
   echo "-----------------"
   echo " DUPLICATIONS    "
   echo "-----------------"
   clonedigger --cpd-output -o $SITE_JOB/cpd.xml --ignore-dir=./tests .
   }

function pylintMetrics(){
   echo "-----------------"
   echo " METRICAS PYLINT "
   echo "-----------------"
   for f in `find . -name *.py|egrep -v '^./tests/'`; do
      pylint --output-format=parseable --rcfile=$pythonDirConf/conf/pylint.rc --reports=y $f >> $SITE_JOB/pylint.log
   done || :
}

function pyFlakes(){
   echo "-------------------"
   echo " METRICAS PYFLAKES "
   echo "-------------------"
   find . -name *.py|egrep -v '^./tests/'|xargs pyflakes  > $SITE_JOB/pyflakes.log || :
}

function countLines(){
   echo "-------------------"
   echo " SLOCCOUNT "
   echo "-------------------"
   sloccount --duplicates --wide --details .|grep -v "tests"|grep -v "target" > $SITE_JOB/sloccount.sc
}


function cyclomaticComplexity(){
   cd $JOB_HOME
   echo "SiteJob es: " $SITE_JOB
   for i in `find . -name "*.py"|grep -v "__"|grep -v "setup.py"|grep -v "/tests/"`; do pymetrics $i -i mccabe:McCabeMetric -B -S -C; done > $SITE_JOB/pymetrics.txt
   
   cp $SITE_JOB/pymetrics.txt $SITE_JOB/CyclomaticComplexity.txt

   max_cc=5
   warnings=0
   > ccWarnings.txt
   while read line
    do
      file=`echo $line | grep "McCabe Complexity Metric for file" | cut -d' ' -f6`
      file=${file/ /}
      if [ -n "$file" ]
      then
        last_file=$file
      else
        cc=`echo $line | awk '{ print $1 }' | egrep "^[0-9]+$"`
        if [ -n "$cc" ]
        then
         if [ "$cc" -gt "$max_cc" ]
          then
            module=`echo $line | awk '{ print $2 }'`
            ((++warnings))
            echo "WARNING CC="$cc": " $last_file "("$module")" >> ccWarnings.txt
          fi
        fi
       fi
     done < $SITE_JOB/CyclomaticComplexity.txt
   echo "All CC warnings:" $warnings > $SITE_JOB/CyclomaticComplexity.txt
   echo "" >> $SITE_JOB/CyclomaticComplexity.txt
   cat ccWarnings.txt >> $SITE_JOB/CyclomaticComplexity.txt
   cat $SITE_JOB/CyclomaticComplexity.txt
   rm -Rf ccWarnings.txt
}

function pythonDocs() {
   echo "---------------"
   echo " DOCUMENTACION "
   echo "---------------"
   mkdir -p $SITE_JOB/docs
   for i in `find . -name "*.py"|grep -v "setup.py"`;do
         epydoc -o $SITE_JOB/docs $i
   done;
}

##### main
init "$*"
isExecutedInDevelenv
unitTest
if [ -f "setup.py" ]; then
   package
   rpmInfo
fi

if [ "$isDevelenv" == "true" ]; then
   metrics
fi
duplications
pylintMetrics
pyFlakes
cyclomaticComplexity 
countLines
pythonDocs
echo "---------------"
echo "      END      "
echo "---------------"

